package com.university.mentalhealthplatform.studentV3.service;

import com.university.mentalhealthplatform.appModels.Detail;
import com.university.mentalhealthplatform.studentV3.repository.DetailRepositoryV3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DetailServiceV3 {

    @Autowired
    private DetailRepositoryV3 detailRepositoryV3;

    // 获取所有详细信息
    public List<Detail> getAllDetails() {
        return detailRepositoryV3.findAll();
    }

    // 根据类型获取详细信息
    public Detail getDetailByType(String type) {
        return detailRepositoryV3.findByType(type);
    }

    // 保存新的详细信息
    public Detail saveDetail(Detail detail) {
        return detailRepositoryV3.save(detail);
    }

    // 更新现有的详细信息
    public Detail updateDetail(String type, Detail detail) {
        Detail existingDetail = detailRepositoryV3.findByType(type);

        if (existingDetail != null) {
            existingDetail.setTitle(detail.getTitle());
            existingDetail.setDescription(detail.getDescription());
            return detailRepositoryV3.save(existingDetail);
        } else {
            return null; // 如果未找到相应的详细信息，返回 null
        }
    }

    // 删除详细信息
    public boolean deleteDetail(String type) {
        Detail existingDetail = detailRepositoryV3.findByType(type);

        if (existingDetail != null) {
            detailRepositoryV3.delete(existingDetail);
            return true; // 删除成功
        }
        return false; // 未找到相应的详细信息，无法删除
    }

    public String getIntroductionText() {
        Detail introductionDetail = detailRepositoryV3.findByType("introduction");
        if (introductionDetail!= null) {
            return introductionDetail.getDescription();
        }
        return "";
    }
}

